package org.cboard.services;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.cboard.dao.DatasetDao;
import org.cboard.pojo.DashboardDataset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yfyuan on 2016/10/11.
 */
@Repository
public class DatasetService {

    @Autowired
    private DatasetDao datasetDao;

    public ServiceStatusWithData<?> save(String userId, String json) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        DashboardDataset dataset = new DashboardDataset();
        dataset.setUserId(userId);
        dataset.setName(jsonObject.getString("name"));
        dataset.setData(jsonObject.getString("data"));
        dataset.setCategoryName(jsonObject.getString("categoryName"));
        if (StringUtils.isEmpty(dataset.getCategoryName())) {
            dataset.setCategoryName("默认分类");
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("dataset_name", dataset.getName());
        paramMap.put("user_id", dataset.getUserId());
        paramMap.put("category_name", dataset.getCategoryName());
        if (datasetDao.countExistDatasetName(paramMap) <= 0) {
            datasetDao.save(dataset);
            return new ServiceStatusWithData<>(ServiceStatus.Status.Success, "成功").data(dataset);
        } else {
            return new ServiceStatusWithData<>(ServiceStatus.Status.Fail, "名称已存在");
        }
    }

    public ServiceStatusWithData<?> update(String userId, String json) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        DashboardDataset dataset = new DashboardDataset();
        dataset.setUserId(userId);
        dataset.setId(jsonObject.getLong("id"));
        dataset.setName(jsonObject.getString("name"));
        dataset.setCategoryName(jsonObject.getString("categoryName"));
        dataset.setData(jsonObject.getString("data"));
        if (StringUtils.isEmpty(dataset.getCategoryName())) {
            dataset.setCategoryName("默认分类");
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("dataset_name", dataset.getName());
        paramMap.put("user_id", dataset.getUserId());
        paramMap.put("dataset_id", dataset.getId());
        paramMap.put("category_name", dataset.getCategoryName());
        if (datasetDao.countExistDatasetName(paramMap) <= 0) {
            datasetDao.update(dataset);
            return new ServiceStatusWithData<>(ServiceStatus.Status.Success, "成功").data(dataset);
        } else {
            return new ServiceStatusWithData<>(ServiceStatus.Status.Fail, "名称已存在");
        }
    }

    public ServiceStatus delete(String userId, Long id) {
        datasetDao.delete(id, userId);
        return new ServiceStatus(ServiceStatus.Status.Success, "成功");
    }

    public ServiceStatus checkDataSourceIsStillDependent( String userId, Long dataSourceId) {
        List<DashboardDataset> datasetList = datasetDao.getDatasetList(userId);
        for (DashboardDataset dashboardDataset : datasetList) {
            String dataSetJsonConfig = dashboardDataset.getData();
            if (!StringUtils.isEmpty(dataSetJsonConfig)) {
                //获取对应的数据源id
                JSONObject dataSourceConfig = JSON.parseObject(dataSetJsonConfig);
                if (dataSourceConfig.get("datasource") != null) {
                    String dataSourceIdJSON = dataSourceConfig.get("datasource").toString();
                    if (dataSourceId.toString().equals(dataSourceIdJSON)) {
                        return new ServiceStatus(ServiceStatus.Status.Fail, "删除失败，请检查对应的数据源依赖配置");
                    }
                }
            }
        }
        return new ServiceStatus(ServiceStatus.Status.Success, "");
    }
}
